************************************************************************
* Generate country-specific information friction
* read market fe file, generate phi =  -rho/sigma*theta/(1+w_uj/w_theta)
************************************************************************


tempfile revtop
use "transactions.dta", clear
bys buyercountry: egen rev = sum(transactionvalue)
duplicates drop buyercountry, force
keep buyercountry rev
save `revtop'


import delimited online-fe-ratio_feonly_corrected.csv, clear
merge 1:1 buyercountry using `revtop'
keep if _merge==3
drop _merge
merge 1:1 buyercountry using "distcodegdp.dta"
keep if _merge==3
drop _merge
merge m:1 numcountrycode using "chnmigrants2015.dta"
keep if _merge==3
drop _merge
merge m:1 numcountrycode using "2015worldpop.dta"
keep if _merge==3
drop _merge

gen chnmshare = chnmigrants/(pop2015thousand*1000)
gen chinargdppc = 6069.7207
gen rgdppcdif = gdpperc2012usd - chinargdppc
gen absrgdppcdif = abs(rgdppcdif)
gen lnabsrgdppcdif = ln(absrgdppcdif)
gen lndistw = ln(distw)

gen phi = -.124*lndistw + 0.029*lnabsrgdppcdif +1.013*chnmshare
keep if phi!=.
gsort -rev
//gen rank = _n
//keep if rank<=10
//drop rank rev
keep buyercountry aliexpress_ratio countryefra chinapost phi lndistw lnabsrgdppcdif chnmshare
egen sumaliexpress_ratio = sum(aliexpress_ratio)
replace aliexpress_ratio = aliexpress_ratio/sumaliexpress_ratio
drop sumaliexpress_ratio

export delimited "online-fe-ratio_feonly_corrected_wjcountry_all.csv", replace


